Java JavaScript Python C# C C++ Go Kotlin PHP Swift R Ruby TypeScript Scala SQL Perl rust VisualBasic Matlab Julia

Python Functions → keyword arguments

Python Functions

keyword arguments

Keyword arguments (kwargs) in Python offer a powerful and flexible way to pass arguments to functions. Unlike positional arguments, which are assigned based on their order in the function call, keyword arguments are explicitly identified by their names. This enhances code readability, makes functions more robust to changes, and simplifies the handling of optional parameters.

Basic Syntax and Functionality

When defining a function, you can specify parameters that accept keyword arguments. The caller then provides the arguments using the `parameter_name=value` syntax.
Keyword arguments basic syntax def greet(name, greeting="Hello"): print(f"{greeting}, {name}!") greet("Alice") greet("Bob", greeting="Good morning") greet(name="Charlie", greeting="Howdy")

Output

Hello, Alice! Good morning, Bob! Howdy, Charlie!
In this example, `name` is a positional argument (required), and `greeting` is a keyword argument with a default value. The function call demonstrates that: ✦ You can omit keyword arguments if they have defaults. ✦ You can override default values by explicitly providing the keyword argument. ✦ The order of keyword arguments in the function call doesn't matter.

Advantages of Keyword Arguments

Improved Readability: Keyword arguments make the code self-documenting. It's immediately clear what each argument represents. Flexibility and Extensibility: Adding new optional parameters to a function becomes easier without breaking existing code. Only the calling code needs updating to use the new kwargs. Reduced Errors: Keyword arguments reduce the risk of positional argument errors, especially in functions with many parameters. The interpreter directly associates the value with the correct parameter. Default Values: Default values assigned to keyword arguments provide sensible defaults and simplify function calls.

Multiple Keyword Arguments and Mixing with Positional Arguments

You can have multiple keyword arguments in a function definition and even mix them with positional arguments. Positional arguments *must* come before keyword arguments in the function call.
Multiple Keyword Arguments example def describe_person(name, age, city="Unknown", occupation="Unknown"): print(f"Name: {name}, Age: {age}, City: {city}, Occupation: {occupation}") describe_person("David", 30) # Uses defaults for city and occupation describe_person("Eva", 25, city="London", occupation="Engineer") describe_person("Frank", 40, occupation="Teacher", city="Paris") # Keyword order flexible

Output

Name: David, Age: 30, City: Unknown, Occupation: Unknown Name: Eva, Age: 25, City: London, Occupation: Engineer Name: Frank, Age: 40, City: Paris, Occupation: Teacher

Arbitrary Keyword Arguments (`**kwargs`)

The `**kwargs` syntax allows a function to accept an arbitrary number of keyword arguments. These arguments are collected into a dictionary.
Arbitrary Keyword Arguments (`**kwargs`) def print_details(**kwargs): for key, value in kwargs.items(): print(f"{key}: {value}") print_details(name="Grace", age=28, country="Canada", skill="Python")

Output

name: Grace age: 28 country: Canada skill: Python
This is extremely helpful when you don't know beforehand how many keyword arguments a function might receive, for instance, when building a highly customizable function or working with configuration data. Important Note: You cannot mix positional-only and keyword-only arguments directly in the same function definition (before Python 3.8). For more fine-grained control over argument passing style (positional-only or keyword-only), refer to Python's features introduced in 3.8 and later versions.

Tutorials